const express = require("express")
const path = require("path")
const { readFileSync } = require("fs")

const port = 3000
const app = express()

//处理跨域头
app.all('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-method', 'POST,GET');
    next()
})

app.get('/images/:num', (req, res) => {
    const num = req.params.num
    res.sendFile(path.resolve(__dirname, './images/' + `${num}`))
})

app.get('/imgs', (req, res) => {
    const imageData = JSON.parse(readFileSync(path.resolve(__dirname, './data/images.json'), 'utf-8'))
    res.send(imageData)
})

app.listen(port, () => {
    console.log("listening port 3000")
})